/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.ARTesting;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Date;

import android.location.Location;
import android.util.Log;

import com.LibreGeoSocial.App.Utils.LocationUtils;

public class TestingManager{
	private static final String DIR = "LGS_AR_SAMPLES";
	
	private static Location mLoc = new Location("");
	private static Location resRealLoc = new Location("");
	private static Location resLoc = new Location("");
	private static float azimuth;
	private static float inclination;
	private static long initTime;
	private static long finalTime;
	
	public static void reset(){
		mLoc.reset();
		resLoc.reset();
		resRealLoc.reset();
		azimuth = 1000;
		inclination = 1000;
		initTime = 0;
		finalTime = 0;
	}
	
	public static void resetRealLocation(){
		resRealLoc = null;
	}
	
	public static void setUserLoc(float latitude, float longitude, float altitude){
		mLoc = new Location("");
		mLoc.setLatitude(latitude);
		mLoc.setLongitude(longitude);
		mLoc.setAltitude(altitude);
	}
	
	public static void setResLoc(float latitude, float longitude, float altitude){
		resLoc = new Location("");
		resLoc.setLatitude(latitude);
		resLoc.setLongitude(longitude);
		resLoc.setAltitude(altitude);
	}
	
	public static void setResRealLoc(float latitude, float longitude, float altitude){
		resRealLoc = new Location("");
		resRealLoc.setLatitude(latitude);
		resRealLoc.setLongitude(longitude);
		resRealLoc.setAltitude(altitude);
	}
	
	public static void setAzimuth(float azimuth){
		TestingManager.azimuth = azimuth;
	}
	
	public static void setInclination (float inclination){
		TestingManager.inclination = inclination;
	}
	
	public static void setInitTime(){
		Date date = new Date();
		initTime = date.getTime();
	}
	
	public static void setFinalTime(){
		Date date = new Date();
		finalTime = date.getTime();
	}
	
	public static Location getUserLocation(){
		return mLoc;
	}
	
	public static Location getResLocation(){
		return resLoc;
	}
	
	public static Location getResRealLocation(){
		return resRealLoc;
	}
	
	public static float getAzimuth(){
		return azimuth;
	}
	
	public static float getInclination(){
		return inclination;
	}
	
	public static float getElapsedTime(){
		return (finalTime - initTime);
	}
	
	public static double getHeightError(){
		return  (resLoc.getAltitude() - resRealLoc.getAltitude());
	}
	
	public static double getRealAzimuth(){
		return LocationUtils.calculateResourceLocAzimuth(mLoc, resRealLoc);
	}
	
	public static String writeSample(String tag_name, String res_name){
		
		String status = "Error";
		if(resRealLoc==null)
			return status;
		
		String data = "";
		
		String name = "";
		
		name += tag_name + "_";
		name += res_name + "_";
		name += changeFormat(Float.toString(mLoc.distanceTo(resRealLoc))) + "_";
		name += changeFormat(Double.toString(resRealLoc.getAltitude() - mLoc.getAltitude())) + ".txt";

		data += tag_name + "\t";
		data += res_name + "\t";
		data += changeFormat(Float.toString(mLoc.distanceTo(resRealLoc))) + "\t";
		data += changeFormat(Float.toString(mLoc.distanceTo(resLoc))) + "\t";
		data += changeFormat(Float.toString(resRealLoc.distanceTo(resLoc))) + "\t";
		data += changeFormat(Double.toString(getRealAzimuth())) + "\t";
		data += changeFormat(Float.toString(azimuth)) + "\t";
		data += changeFormat(Double.toString(resRealLoc.getAltitude())) + "\t";
		data += changeFormat(Double.toString(resLoc.getAltitude())) + "\t";
		data += changeFormat(Float.toString(LocationUtils.calculateRoll(mLoc.distanceTo(resRealLoc), 
				(float)(resRealLoc.getAltitude())))) + "\t";
		data += changeFormat(Float.toString(inclination)) + "\t";
		data += changeFormat(Float.toString(getElapsedTime())) + "\n";
		
		Log.d(name, data);

		// write to SD card
		try{
			File dir = new File("/sdcard/" + DIR );
			if (!dir.exists())
				dir.mkdir();

			File outputFile = new File("/sdcard/" + DIR + "/" + name); 
			if(outputFile.exists()){
				outputFile.delete();
				outputFile = new File("/sdcard/" + DIR + "/" + name);
			}

			PrintWriter out = new PrintWriter(new FileOutputStream(outputFile,true)); 
			out.write(data);
			out.flush();
			out.close();
			status = "Ok";
		}catch(Exception e){
			Log.e("SaveSamples", e.toString());
		}
		
		return status;
	}
	
	private static String changeFormat(String number){
		String num = number;
		if(num.contains(".")){
			String[] parts = num.split("\\.");
			num = parts[0] + "," + parts[1];
		}
		
		return num;
	}
	
}